import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:smart_translator/app/event/DataCallBack.dart';
import 'package:smart_translator/app/modules/home/controllers/home_controller.dart';
import 'package:smart_translator/app/style/widget_style.dart';
import 'package:smart_translator/app/widgets/AppHeader.dart';
import 'package:smart_translator/app/widgets/CommonWidgets.dart';
import '../../routes/app_pages.dart';
import '../../style/app_colors.dart';
import '../../utils/tips.dart';
import '../controller/IndexViewController.dart';

class IndexViewView extends GetView<IndexViewController> {
  IndexViewView({super.key});
  FocusNode inputFocusNode =  FocusNode();



  @override
  Widget build(BuildContext context) {
    inputFocusNode.addListener(() {
      if (inputFocusNode.hasFocus) {
        controller.goToScriptInputPage();
        inputFocusNode.unfocus();
      }
    });

    return Scaffold(
        body: SafeArea(
            top: false,
            child: Stack(
          children: [
            Container(
              child: SvgPicture.asset(
                "assets/images/header_part_bg.svg",
                width: Get.width,
              ),
            ),
            Container(child: Column(children: [
              AppHeader.obtainHeaderForHomePage(),
              Expanded(child:
              SingleChildScrollView(
                  child: Container(
                      child: Obx(()=>Container(
                        width: Get.width,
                        padding:  EdgeInsets.only(left: 20.w,right: 20.w),
                        child: Column(
                          children: [
                            Container(
                              decoration: BoxDecoration(
                                boxShadow: [
                                   CommonWidgets.getBoxShadow(),
                                ],
                                color:Colors.white,
                                borderRadius: BorderRadius.circular(8), // Optional: add border radius
                              ),
                              child: Column(
                                mainAxisSize: MainAxisSize.min,
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  // 输入框
                                  InkWell(
                                    onTap: () {
                                      controller.goToScriptInputPage();
                                    },
                                    child: Container(
                                      width: Get.width,
                                      height: 65.h,
                                      padding: EdgeInsets.only(left:10.w,right: 10.w,top: 10.h),
                                      child: SingleChildScrollView(
                                        controller: controller.singleScrollController.value,
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          mainAxisSize: MainAxisSize.min,
                                          children: [
                                            controller.audioService.indexPageFinalFromLanguage.value.trim() != '' ? controller.onlyShowscript.value ? SizedBox() : Text(controller.audioService.indexPageFinalFromLanguage.value,style: TextStyle(
                                                fontSize: 15.sp
                                            ),):
                                            Text('开始输入...',style: TextStyle(
                                                color: AppColors.hint_text_color.withValues(alpha: 0.4),
                                                fontSize: 15.sp
                                            ),),
                                            controller.audioService.indexPageFinalFromLanguage.value.trim() != ''? controller.onlyShowscript.value? SizedBox(): CommonWidgets.getMyDivider(): SizedBox(),
                                            controller.audioService.indexPageFinalToLanguage.value.trim() != '' ? Text(controller.audioService.indexPageFinalToLanguage.value,style: TextStyle(
                                                fontSize: 15.sp
                                            )):  SizedBox()

                                          ],
                                        ),
                                      ),
                                    ),
                                  ),
                                  Container(
                                    padding: EdgeInsets.symmetric(horizontal: 10.w),
                                    margin: EdgeInsets.only(bottom: 5.h,top: 5.h),
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        InkWell(
                                          onTap: () {
                                            Get.toNamed(Routes.HISTORY_RECORDS,parameters: {
                                              'source':'directTranslation'
                                            });
                                          },
                                          child: Row(
                                            children: [
                                              Image.asset('assets/images/icon_index_record.png',width: 16.w,height: 16.h),
                                              SizedBox(width: 4),
                                              Text('记录', style: AppStyleThemeData.linkStyle),
                                            ],
                                          ),
                                        ),
                                        InkWell(
                                          onTap: () {
                                             controller.responseAudio();
                                          },
                                          child: Container(
                                            padding: EdgeInsets.all(8.w),
                                            child: controller.audioService.isPlaying.value ? SvgPicture.asset('assets/images/icon_index_speaking.svg',width: 18.w,height: 23.h):
                                            SvgPicture.asset('assets/images/icon_index_speaking.svg',width: 18.w,height: 23.h,color: Colors.grey,),
                                          ),
                                        ),
                                        controller.audioService.indexPageFinalToLanguage.value != '' ?
                                        InkWell(
                                          onTap: () {
                                            Get.toNamed(Routes.LANDSCAPE_DIRECT_TRANS);
                                          },
                                          child: Container(
                                            padding: EdgeInsets.all(8.w),
                                            child: Image.asset('assets/images/icon_full_screen.png',width: 16.w,height: 16.h),
                                          ),
                                        ): SizedBox(width: 50.w,)
                                      ],
                                    ),
                                  )
                                ],
                              ),
                            ),
                            SizedBox(height: 16.h),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                InkWell(
                                  onTap: () {
                                    controller.chooseFromLanguages();
                                  },
                                  child: Container(
                                    width: 128.w,
                                    height: 48.h,
                                    padding: EdgeInsets.all(8.w),
                                    decoration: BoxDecoration(
                                      boxShadow: [
                                        CommonWidgets.getBoxShadow(),
                                      ],
                                      color:Colors.white,
                                      borderRadius: BorderRadius.circular(8), // Optional: add border radius
                                    ),
                                    child: Center(
                                      child: Text(
                                        '${controller.audioService.fromLanFlagDisplay.value}',style: AppStyleThemeData.normal_bold,
                                        maxLines: 1, // 最多显示2行
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    ),


                                  ),
                                ),
                                InkWell(
                                  onTap: () {
                                    controller.audioService.exchangeLanguages(callBack: DataCallBack(
                                        businessCallBack: (v) {
                                          controller.changeLanguageReInitial();
                                        }
                                    ));
                                  },
                                  child: Container(
                                    height: 48.h,
                                    width: 50.w,
                                    padding: EdgeInsets.all(14.w),
                                    decoration: BoxDecoration(
                                      boxShadow: [
                                        CommonWidgets.getBoxShadow(),
                                      ],
                                      color:Colors.white,
                                      borderRadius: BorderRadius.circular(8), // Optional: add border radius
                                    ),
                                    child: Image.asset('assets/images/icon_index_lang_exchange.png'),
                                  ),
                                ),
                                InkWell(
                                  onTap: () {
                                    controller.chooseToLanguages();
                                  },
                                  child: Container(
                                    padding: EdgeInsets.only(left: 10.w),
                                    width: 126.w,
                                    height: 48.h,
                                    decoration: BoxDecoration(
                                      boxShadow: [
                                        CommonWidgets.getBoxShadow(),
                                      ],
                                      color:Colors.white,
                                      borderRadius: BorderRadius.circular(8), // Optional: add border radius
                                    ),
                                    child: Center(
                                      child: Text('${controller.audioService.toLanFlagDisplay.value.trim()}',
                                        style: AppStyleThemeData.normal_bold,
                                        maxLines: 1, // 最多显示2行
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    ),
                                  ),
                                ),
                                /*InkWell(
                                  onTap: () {
                                    controller.goToScriptInputPage2();
                                  },
                                  child: Container(
                                    padding: EdgeInsets.all(14.w),
                                    decoration: BoxDecoration(
                                        shape: BoxShape.circle,
                                        boxShadow: [
                                          BoxShadow(
                                            color: Colors.black.withValues(alpha: 0.1),
                                            spreadRadius: 2.w,
                                            blurRadius: 7.w,
                                            offset: Offset(0, 8.sp), // changes position of shadow
                                          ),
                                        ],
                                        color: controller.audioService.indexPageFinalFromLanguage.value != '' ? AppColors.app_base_theme_color: Colors.white
                                    ),
                                    child: Image.asset(controller.audioService.indexPageFinalFromLanguage.value != '' ?
                                    'assets/images/icon_index_arrows_white.png': 'assets/images/icon_index_trans_arrows.png',width: 20.w,height: 20.h,),
                                  ),
                                ),*/
                              ],
                            ),
                            SizedBox(height: 16.h),
                            Container(
                              padding: EdgeInsets.only(left: 10.w,right: 10.w),
                              decoration: BoxDecoration(
                                boxShadow: [
                                  CommonWidgets.getBoxShadow(),
                                ],
                                color:Colors.white,
                                borderRadius: BorderRadius.circular(8), // Optional: add border radius
                              ),
                              child: Column(
                                children: [
                                  SizedBox(height: 10.h,),
                                  Container(
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        Row(
                                          children: [
                                            Image.asset('assets/images/icon_off_line_mode.png',width: 24.w,height: 24.h),
                                            SizedBox(width: 7.w,),
                                            Text('离线模式',style: TextStyle(
                                                color: Colors.black,
                                                fontSize: 16.sp
                                            ),)
                                          ],
                                        ),
                                        Container(
                                          height: 24.h,
                                          child: Switch(
                                              value: controller.offlineMode.value,
                                              onChanged: (v) {
                                                controller.offlineMode.value = v;
                                                controller.updateSettingData();
                                              },
                                              inactiveTrackColor: AppColors.swith_inactive_bg_color,
                                              activeTrackColor: AppColors.app_base_theme_color,
                                              trackOutlineWidth: WidgetStateProperty.all(-1)
                                          ),
                                        )


                                      ],
                                    ),
                                  ),
                                  SizedBox(height: 5.h,),
                                  Divider(thickness: 1, color: AppColors.device_page_bg_color),
                                  SizedBox(height: 5.h,),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Row(
                                        children: [
                                          Image.asset('assets/images/only_show_transcript.png',width: 24.w,height: 24.h),
                                          SizedBox(width: 7.w,),
                                          Text('只显示译文',style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 16.sp
                                          ),)
                                        ],
                                      ),
                                      Container(
                                        height: 24.h,
                                        child: Switch(value: controller.onlyShowscript.value, onChanged: (v) {
                                          controller.onlyShowscript.value = v;
                                          controller.updateSettingData();
                                        },
                                            inactiveTrackColor: AppColors.swith_inactive_bg_color,
                                            activeTrackColor: AppColors.app_base_theme_color,
                                            trackOutlineWidth: WidgetStateProperty.all(-1)
                                        ),
                                      )


                                    ],
                                  ),
                                  SizedBox(height: 5.h,),
                                  Divider(thickness: 1,color: AppColors.device_page_bg_color),
                                  SizedBox(height: 5.h,),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Row(
                                        children: [
                                          Image.asset('assets/images/icon_allowother_putin.png',width: 24.w,height: 24.h),
                                          SizedBox(width: 7.w,),
                                          Text('允许其他应用输入',style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 16.sp
                                          ),)
                                        ],
                                      ),
                                      Container(
                                        height: 24.h,
                                        child: Switch(
                                            value: controller.allowOtherAppInput.value,
                                            onChanged: (v) {
                                              controller.allowOtherAppInput.value = v;
                                              controller.updateSettingData();
                                            },
                                            inactiveTrackColor: AppColors.swith_inactive_bg_color,
                                            activeTrackColor: AppColors.app_base_theme_color,
                                            trackOutlineWidth: WidgetStateProperty.all(-1)

                                        ),
                                      )


                                    ],
                                  ),
                                  SizedBox(height: 5.h,),
                                  Divider(thickness: 1,color: AppColors.device_page_bg_color),
                                  SizedBox(height: 5.h,),
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Row(
                                        children: [
                                          Image.asset('assets/images/icon_auto_pronounce.png',width: 24.w,height: 24.h),
                                          SizedBox(width: 7.w,),
                                          Text('自动播报',style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 16.sp
                                          ),)
                                        ],
                                      ),
                                      Container(
                                          height: 24.h,
                                          child: Switch(value: controller.autoPronounce.value, onChanged: (v) {
                                            controller.autoPronounce.value = v;
                                            controller.updateSettingData();
                                          },
                                          inactiveTrackColor: AppColors.swith_inactive_bg_color,
                                          activeTrackColor: AppColors.app_base_theme_color,
                                          trackOutlineWidth: WidgetStateProperty.all(-1),
                                        ),
                                      )


                                    ],
                                  ),
                                  SizedBox(height: 5.h,),
                                  Divider(thickness: 1,color: AppColors.device_page_bg_color),
                                  SizedBox(height: 5.h,),
                                  InkWell(
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        Row(
                                          children: [
                                            Text('声音',style: TextStyle(
                                                color: Colors.black,
                                                fontSize: 16.sp
                                            ),)
                                          ],
                                        ),
                                        Container(
                                          child: Row(
                                            children: [
                                              Text(controller.voiceType.value ==1 ? '男生' : '女生',style: AppStyleThemeData.HINT_TEXT_STYLE_medium,),
                                              SizedBox(width: 5.w,),
                                              Image.asset('assets/images/icon_index_arrow_right.png',width: 20.w,height: 20.h),
                                            ],
                                          ),
                                        )


                                      ],
                                    ),
                                    onTap: () {
                                      controller.showpoupWindowVoiceType(context);
                                    },
                                  ),
                                  SizedBox(height: 15.h),
                                  InkWell(
                                    onTap: () {
                                      controller.showpoupWindowSpeed(context);
                                    },
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        Row(
                                          children: [
                                            Text('语速',style: TextStyle(
                                                color: Colors.black,
                                                fontSize: 16.sp
                                            ),)
                                          ],
                                        ),
                                        Container(
                                          child: Row(
                                            children: [
                                              Text(controller.voidSpeed == 10 ? '正常': controller.voidSpeed == 5 ? '超慢':controller.voidSpeed == 20 ? '超快': '未知',style: AppStyleThemeData.HINT_TEXT_STYLE_medium,),
                                              SizedBox(width: 5.w,),
                                              Image.asset('assets/images/icon_index_arrow_right.png',width: 20.w,height: 20.h),
                                            ],
                                          ),
                                        )
                                      ],
                                    ),
                                  ),
                                  SizedBox(height: 10.h),

                                ],
                              ),
                            ),
                            SizedBox(height: 16.h),
                            Row(
                              children: [
                                InkWell(
                                  onTap: () {
                                    if (controller.audioService.isPlaying.value) {
                                        Tips.showToast('正在录音中,请先关闭录音后尝试.');
                                        return;
                                    }
                                    Get.toNamed(Routes.REAL_TRANSLATION);
                                  },
                                  child: Container(
                                    padding: EdgeInsets.only(left: 52.w,right: 52.w,top: 26.h,bottom: 26.h),
                                    decoration: BoxDecoration(
                                      boxShadow: [
                                        CommonWidgets.getBoxShadow(),
                                      ],
                                      color:Colors.white,
                                      borderRadius: BorderRadius.circular(8), // Optional: add border radius
                                    ),
                                    child: Column(
                                      children: [
                                        Image.asset('assets/images/icon_real_voice_trans.png',width: 56.w,height: 56.h),
                                        Text('同声翻译',style: AppStyleThemeData.normal_bold)
                                      ],
                                    ),
                                  ),
                                ),
                                SizedBox(width: 16.w,),
                                Column(
                                  children: [
                                    InkWell(
                                      onTap: () {
                                        if (controller.audioService.isPlaying.value) {
                                          Tips.showToast('正在录音中,请先关闭录音后再使用此功能');
                                          return;
                                        }
                                        Get.toNamed(Routes.LISTENER_TRANSLATION);
                                      },
                                      child: Container(
                                        height: 55.h,
                                        padding: EdgeInsets.only(left: 16.w,right: 63.w),
                                        decoration: BoxDecoration(
                                          boxShadow: [
                                            CommonWidgets.getBoxShadow(),
                                          ],
                                          color:Colors.white,
                                          borderRadius: BorderRadius.circular(8), // Optional: add border radius
                                        ),
                                        child: Row(
                                          children: [
                                            Image.asset('assets/images/icon_listening_trans.png',width: 40.w,height: 40.h),
                                            SizedBox(width: 10.w,),
                                            Text('听译',style: AppStyleThemeData.normal_bold)
                                          ],

                                        ),
                                      ),
                                    ),
                                    SizedBox(height: 16.h,),
                                    Container(
                                      height: 55.h,
                                      padding: EdgeInsets.only(left: 16.w,right: 33.w),
                                      decoration: BoxDecoration(
                                        boxShadow: [
                                          CommonWidgets.getBoxShadow(),
                                        ],
                                        color:Colors.white,
                                        borderRadius: BorderRadius.circular(8), // Optional: add border radius
                                      ),
                                      child:
                                        InkWell(
                                          onTap: () {
                                            Get.toNamed(Routes.PHOTO_TRANSLATE);
                                        },
                                         child: Row(
                                           children: [
                                              Image.asset('assets/images/icon_take_picture_trans.png',width: 40.w,height: 40.h),
                                              SizedBox(width: 10.w,),
                                              Text('拍照翻译',style: AppStyleThemeData.normal_bold)
                                              ],
                                           ) ,
                                      ),
                                    ),

                                  ],
                                ),

                              ],
                            )
                          ],
                        ),
                      )))
              )
              )
            ],),),

          ],
        )),
    );
  }
}

